home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / polipo / polipo-control
Encoding:
Text File  |  2010-05-22  |  2.1 KB  |  105 lines

  1. #!/bin/sh
  2.  
  3. set -e
  4.  
  5. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  6.  
  7. DAEMON=/usr/bin/polipo
  8. test -x $DAEMON || exit 0
  9.  
  10. CONFIG_FILE=/etc/polipo/config
  11. FORBIDDEN_FILE=/etc/polipo/forbidden
  12.  
  13. NAME=polipo
  14.  
  15. PIDFILE=/var/run/$NAME/$NAME.pid
  16. LOGFILE=/var/log/$NAME/$NAME.log
  17. USER=proxy
  18. GROUP=proxy
  19. DAEMON_OPTS="-c $CONFIG_FILE pidFile=$PIDFILE daemonise=true logFile=$LOGFILE"
  20.  
  21. forbiddenFile() {
  22.         if [ -f $FORBIDDEN_FILE ] || [ -d $FORBIDDEN_FILE ]; then
  23.                 echo -n $FORBIDDEN_FILE
  24.         else
  25.                 echo -n /dev/null
  26.         fi
  27. }
  28.  
  29. waitForPIDRemove() {
  30.         T=30
  31.         for i in `seq 1 $T`; do
  32.                 if [ ! -f $PIDFILE ]; then return; fi
  33.                 sleep 1
  34.         done
  35.         echo "Waited $T seconds and $PIDFILE did not disappear.  Giving up." 2>&1
  36. }
  37.  
  38. # Outputs the binary name of the process with PID $1, e.g. /usr/sbin/polipo
  39. nameOfPID() {
  40.     ps -o command= -p $1 | cut -f1 '-d '
  41. }
  42.  
  43. # Returns true if the pidfile exists, and there is a polipo process running
  44. # with the PID therein.  False otherwise.
  45. alreadyRunning() {
  46.     if [ -r $PIDFILE ]; then
  47.         PID=`cat $PIDFILE`
  48.         if [ "`nameOfPID $PID`" = $DAEMON ]; then
  49.             return 0 # TRUE
  50.         fi
  51.     fi
  52.     return 1 # FALSE
  53. }
  54.  
  55. # Deletes polipo's pidfile if it exists
  56. # This is designed to remove the pidfile from a crashed polipo before
  57. # starting a new one
  58. removePIDFile() {
  59.     if [ -e $PIDFILE ]; then
  60.         echo -n "Removing stale pidfile"
  61.         rm $PIDFILE
  62.         echo "."
  63.     fi
  64. }
  65.  
  66. DAEMON_OPTS="$DAEMON_OPTS forbiddenFile=`forbiddenFile`"
  67.  
  68. polipo_start() {
  69.  
  70.     if alreadyRunning; then
  71.         echo "$DAEMON already running -- doing nothing"
  72.         exit
  73.     fi    
  74.  
  75.     removePIDFile
  76.  
  77.         start-stop-daemon --start --quiet --pidfile $PIDFILE \
  78.                 --chuid $USER:$GROUP --exec $DAEMON -- $DAEMON_OPTS
  79. }
  80.  
  81. polipo_stop() {
  82.         start-stop-daemon --stop --quiet --pidfile $PIDFILE \
  83.                 --oknodo --exec $DAEMON
  84. }
  85.  
  86. case "$1" in
  87.   start)
  88.         polipo_start
  89.         ;;
  90.   stop)
  91.         polipo_stop
  92.         ;;
  93.   restart)
  94.         polipo_stop
  95.         waitForPIDRemove
  96.         polipo_start
  97.         ;;
  98.   *)
  99.         N=polipo-control
  100.         echo "Usage: $N {start|stop}" >&2
  101.         exit 1         
  102. esac
  103.  
  104. exit 0
  105.